package com.mobiporter.adias.tags;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.jsp.JspException;

import com.mobiporter.adias.core.Manager;
import com.mobiporter.adias.tags.model.SelectItem;
import com.mobiporter.adias.tags.model.MonthYear;
import com.mobiporter.adias.tags.utils.CollectionsUtils;
import com.mobiporter.adias.tags.utils.HTMLTagGenerator;
import com.mobiporter.utils.DateUtils;

public class DateTag extends MobiTag
{
	
	public DateTag()
	{
		super();
	}
	
	private static final long serialVersionUID = 7661424413901322459L;
	private String startDate ;
	private String monthsAhead ;
	private String selectedDate ;

	@Override
	public int doEndTag() throws JspException 
	{
		return EVAL_PAGE ;
	}

	@Override
	public int doStartTag() throws JspException 
	{
		
		setValue();
		String dayId = getId() + "_day" ;
		String monthId = getId() + "_month";
		
		List<String> dates = DateUtils.getDates();
		List<SelectItem> dayOptions = CollectionsUtils.getHashMapFromLists(dates, dates);
		
		Date today = Calendar.getInstance().getTime() ;
		
		String selectedDay = null ;
		String selectedMonth = null ;
		if (getSelectedDate()==null)
		{
			setSelectedDate(DateUtils.formatDate(today));
		}
		
		selectedDay = DateUtils.getDayFromDate(getSelectedDate()) ;
		selectedMonth = DateUtils.getMonFromDate(getSelectedDate());
		
		if (Manager.isSelectBoxEnabled(getUserAgent()))
		{
			String daySelectHTML = HTMLTagGenerator.getSelectBox(dayOptions, dayId, selectedDay);
			
			Date currentDate = null ;
			
			if (startDate!=null)
			{
				currentDate = DateUtils.getDateFromString(startDate);
			}
			else
			{
				currentDate = today;
			}
			
			List<MonthYear> monthYears = DateUtils.getMonthYearsFrom(currentDate);
			List<SelectItem> monthOptions = new ArrayList<SelectItem>() ;
			
			for (MonthYear monthYear : monthYears)
			{
				String value = monthYear.getMonth()+ "/"+monthYear.getYear() ;
				String name = monthYear.getMonthName()+ " " + monthYear.getYear() ;
				SelectItem m = new SelectItem() ;
				m.setKey(name);
				m.setValue(value);
				monthOptions.add(m);
			}
			
			String monthSelectHTML = HTMLTagGenerator.getSelectBox(monthOptions, monthId, selectedMonth);
			writeOut(daySelectHTML+monthSelectHTML);
		}
		else
		{
			String textBoxHTML = HTMLTagGenerator.getTextBox(getId(),getSelectedDate());
			writeOut(textBoxHTML);
		}
		return SKIP_BODY ;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getMonthsAhead() {
		return monthsAhead;
	}

	public void setMonthsAhead(String monthsAhead) {
		this.monthsAhead = monthsAhead;
	}

	public String getSelectedDate() {
		return selectedDate;
	}

	public void setSelectedDate(String selectedDate) {
		this.selectedDate = selectedDate;
	}

	@Override
	public void setValueToControl(String value) 
	{
		setSelectedDate(value);
	}
}
